//
//  XS_Singleton.swift
//  EKLWisdomP3
//
//  Created by 韩云智 on 2020/10/16.
//

import RxSwift
import Alamofire

enum XS_UserType {
    case user
    case vip
}

class XS_UserBOM: XS_BaseModel {
    var id = 0
    var sourceType = ""
    var sourceId = ""
    var registerTime = ""
    var username = ""
    var phone = ""
    var wechat = ""
    var openId = ""
    var address = ""
    var userImgSrc = ""
    var userType = ""
    var introduce = ""
    var sex = ""
    var birthday = ""
    var score = ""
    var money = ""
    var vipExpirationTime = ""
    var vipStartTime = ""
    var lastBirthdayTime = ""
    var learningTime = ""
    var organizationId = ""
    var createTime = ""
    var createUserId = ""
    var updateTime = ""
    var updateUserId = ""
    var status = ""
    var couponsNumber = ""
    var account = 0
    
    var perfectStatus = ""
    
    var payCurriclumStatus = ""
    
    var industryList : [String] = []
    
    var unreadCount = 0
    
    func userTypeEnum() -> XS_UserType {
        return userType == "user" ? .user : .vip
    }
}
class XS_UserInfoM: XS_BaseModel {
    var ip = ""
    var userBO = XS_UserBOM()
    var token = ""
    var isAnswer = 0
}

class XS_Singleton: NSObject {
    static let shared = XS_Singleton()
    
    override init() {
        XS_NetWork.getParamBlock = { param in // 统一添加参数
            guard let infoModel = XS_Singleton.shared.infoModel else { return param }
            
            return param
        }
        XS_NetWork.getHeadersBlock = { header in // 统一添加 header
            guard let infoModel = XS_Singleton.shared.infoModel else { return header }
            var header = header
            header["Content-Type"] = "application/json"
            header["token"] = infoModel.token
            // "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOiIxNjYxOTk2NjIxNCJ9.gr7tHBYRNQuEz0LeeL3pC58PWWbWnN2ivvwk0XixOUc"
            return header
        }
        XS_NetWork.filterState = { resp in // 统一过滤code
            print("++++++++++++++++++++++++++++++++")
            print(resp.success)
            print(resp.code)
            print(resp.msg)
            print(resp.data)
            print("++++++++++++++++++++++++++++++++")
            switch resp.code {
            case "00002":
                XS_NVC.shared.toLogin(true)
                return false
            default:
                return true
            }
        }
    }
    
    var settingModel : XS_SettingModel?
    
    private var _videoAllowCellar: Bool = false
    var videoAllowCellar: Bool {
        get {
            let value = UserDefaults.standard.bool(forKey: .xsKey_videoAllowCellar)
            return value
        }
        set {
            _videoAllowCellar = newValue
            UserDefaults.standard.set(newValue, forKey: .xsKey_videoAllowCellar)
            UserDefaults.standard.synchronize()
        }
    }
    
    private var _infoModel: XS_UserInfoM?
    var infoModel: XS_UserInfoM? {
        get {
            if _infoModel == nil, let dic = UserDefaults.standard.value(forKey: .xsKey_useInfo) as? [String : Any] {
                _infoModel = JsonUtil.dictionaryToModel(dic, XS_UserInfoM.self)
            }
            return _infoModel
        }
        set {
            _infoModel = newValue
            UserDefaults.standard.set(newValue?.toJSON(), forKey: .xsKey_useInfo)
            changeHome?()
            changeKuaixue?()
        }
    }
    
    var changeHome: (()->())?
    var changeKuaixue: (()->())?
    var changeZhengshu: (([XS_HomeTopM])->())?
    
    lazy var canVideoPS = PublishSubject<Bool>()
    var canVideo: Bool {
        get { isWifi || videoAllowCellar }
    }
    lazy var isWifi = true
    let manager = NetworkReachabilityManager()
    func currentNetReachability() {
        manager?.startListening(onUpdatePerforming: { [weak manager] status in
            switch status {
            case .unknown:
                print("未识别的网络")
            case .notReachable:
                print("不可用的网络(未连接)")
            case .reachable:
                guard let manager = manager else { return }
                if manager.isReachableOnEthernetOrWiFi {
                    print("wifi的网络")
                    self.isWifi = true
                    self.canVideoPS.onNext(self.canVideo)
                } else {
                    print("2G,3G,4G...的网络")
                    self.isWifi = false
                    self.canVideoPS.onNext(self.canVideo)
                }
            }
        })
    }
}
